var app = getApp()

Page({
  data: {
    stations: ['西北工业大学长安校区', '西北工业大学友谊校区', '西安咸阳国际机场', '西安北站','西安站', '韦曲南', '小寨'],
    number:[2, 3, 4, 5],
    start: 0, // 起点    
    end: 1, // 终点
    offDate: '2020-09-01', // 乘车日期   
    offTime: '08:00', // 乘车时间
 
    cap: 1,  // 拼车人数
    pnone: '' // 联系方式
  },



  onSearch(){
    wx.navigateTo({
      url: '../cardlist/cardlist?start='+this.data.stations[this.data.start]+'&end='+this.data.stations[this.data.end]+'&offDate='+this.data.offDate+'&offTime='+this.data.offTime+'&cap='+this.data.number[this.data.cap]+'&phone='+this.data.phone
    })
  },
  onNew(){
    // 检查是否登录
    if(!app.globalData.logged){
      wx.showModal({
        title: '创建失败',
        content: '您还没有登录，请到‘我的’界面登录后再次尝试',
        success: res => {
          console.log("用户未登录，记录创建失败！！！")
        }
      })
      return
    }
    // 检查手机号是否已填写
    if(this.data.phone == ''){
      wx.showModal({
        title: '创建失败',
        content: '您还没有填写手机号，请到‘我的’界面完善后再次尝试',
        success: res => {
          console.log("手机号未填写，记录创建失败！！！")
        }
      })
      return
    }
    var that = this
    // 云数据库初始化
    const db = wx.cloud.database({
      env: 'cloud-dev-alpha-3f1k3'
    })
    // 云数据库添加卡片记录
    var orderID = this.newCardID()
    db.collection('card').add({
      data:{
        cardID: orderID,
        start: this.data.stations[this.data.start],
        end: this.data.stations[this.data.end],
        offDate: this.data.offDate,
        offTime: this.data.offTime,
        cap: this.data.number[this.data.cap],
        phone: [this.data.phone],
        cnt: 1
      },
      // 云数据库添加关系记录
      success: res => {
        db.collection('relation').add({
          data: {
            cardID: orderID
          },
          // 成功发布需求
          success: res=>{
            wx.showToast({
              title: '需求发布成功',
              icon: 'success',
              duration: 2000
            })
            that.setData({
              start: 0,
              end: 1,
              offDate: '2020-09-01',
              offTime: '08:00',
              cap: 1,
              phone: ''
            })
          },
          // 关系记录创建失败，删除卡片，并提示用户
          fail: res=>{
            wx.cloud.callFunction({
              name: 'remove',
              data: {
                cardID: orderID
              },
              success: res=>{
                console.log("调用云函数删除卡片成功！")
              },
              fail: res=>{
                console.log("调用云函数删除卡片失败！！！")
              }
            })
            wx.showModal({
              title: '创建失败',
              content: '拼车需求发布失败，您可以点击"新建"按钮再次尝试',
              success: res=> {
                console.log("小程序端创建记录失败！！！")
              }
            })
          }
        })
      },
      fail: res => {
        wx.showModal({
          title: '创建失败',
          content: '拼车需求发布失败，您可以点击"新建"按钮再次尝试',
          success: res => {
            console.log("小程序端创建记录失败！！！")
          }
        })
      }
    })
  },
  newCardID(){
    var now = new Date()
    // 年、月、日
    var yyyy = now.getFullYear()
    var mm = now.getMonth() + 1
    if(mm < 10){
      mm = "0" + mm
    }
    var dd = now.getDate()
    if(dd < 10){
      dd ="0" + dd
    }
    // 时间戳
    var stamp = now.getTime()

    return yyyy.toString() + mm.toString() + dd.toString() + stamp.toString()
  },

  onChangeStart(event) {
    this.setData({
      start: event.detail.value
    })
     console.log(this.data.start)
  },
  onChangeEnd(event) {
    this.setData({
      end: event.detail.value
    }) 
    console.log(this.data.end)
  },
  onChangeOffDate(event) {
    this.setData({
      offDate: event.detail.value
    })
    console.log(this.data.offDate)
  },
  onChangeOffTime(event) {
    this.setData({
      offTime: event.detail.value
    })
    console.log(this.data.offTime)
  },
  onChangeCap(event) {
    this.setData({
      cap: event.detail.value
    })
    console.log(this.data.cap)
  },
  exchange(){
    var temp = this.data.start
    this.setData({
      start: this.data.end,
      end: temp
    })
  },

  onLoad(){
    try{
      this.data.phone = wx.getStorageSync('phone')
      if(this.data.phone){
        console.log("存储区获取手机号成功！！！")
      }
      else{
        console.log("存储区手机号为空！！！")
      }
    }catch{
      console.log("存储区获取手机号失败！")
    }
  },
  onShow(){
    if(this.data.phone == ''){
      try{
        this.data.phone = wx.getStorageSync('phone')
        console.log("存储区获取手机号成功！！！")
      }catch{
        console.log("存储区获取手机号失败！")
      }
    }
  }
})